Grokking LinkedIn Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
LinkedIn Easy
Find Pivot Index (easy)
Solution: Find Pivot Index
Isomorphic Strings (easy)
Solution: Isomorphic Strings
Second Minimum Node In a Binary Tree (easy)
Solution: Second Minimum Node In a Binary Tree
Minimum Time to Type Word Using Special Typewriter (easy)
Solution: Minimum Time to Type Word Using Special Typewriter
Intersection of Two Arrays (easy)
Solution: Intersection of Two Arrays
Can Place Flowers (easy)
Solution: Can Place Flowers
Valid Perfect Square (easy)
Solution: Valid Perfect Square
Maximum Depth (or Height) of Binary Tree (easy)
Solution: Maximum Depth (or Height) of Binary Tree
Sqrt (easy)
Solution: Sqrt
Valid Palindrome (easy)
Solution: Valid Palindrome
LinkedIn Medium
Rotate List (medium)
Solution: Rotate List
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Valid Triangle Number (medium)
Solution: Valid Triangle Number
Valid Parenthesis String (medium)
Solution: Valid Parenthesis String
Edit Distance (medium)
Solution: Edit Distance
Remove All Ones With Row and Column Flips II (medium)
Solution: Remove All Ones With Row and Column Flips II
Reverse Integer (medium)
Solution: Reverse Integer
Triplet Sum to Zero (medium)
Solution: Triplet Sum to Zero
Longest Substring Without Repeating Characters (medium)
Solution: Longest Substring Without Repeating Characters (medium)
Decode Ways (medium)
Solution: Decode Ways
Quadruple Sum to Target (medium)
Solution: Quadruple Sum to Target
Next Permutation (medium)
Solution: Next Permutation
Search in Rotated Array (medium)
Solution: Search in Rotated Array
Word Search (medium)
Solution: Word Search
Remove Nth Node From End of List (Medium)
Solution: Remove Nth Node From End of List
House Robber II (medium)
Solution: House Robber II
Coin Change
Solution: Coin Change
Design Add and Search Words Data Structure (medium)
Solution: Design Add and Search Words Data Structure
Merge Intervals (medium)
Solution: Merge Intervals
Number of Islands (easy)
Solution: Number of Islands
Longest Palindromic Subsequence (medium)
Solution: Longest Palindromic Subsequence
Kth Smallest Number (medium)
Solution: Kth Smallest Number
Maximum Product Subarray (medium)
Solution: Maximum Product Subarray
Same Tree (medium)
Solution: Same Tree
Max Points on a Line (hard)
Solution: Max Points on a Line
Closest Binary Search Tree Value II (hard)
Solution: Closest Binary Search Tree Value II
Word Ladder (hard)
Solution: Word Ladder
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Largest Rectangle in Histogram (hard)
Solution: Largest Rectangle in Histogram
Serialize and Deserialize Binary Tree (hard)
Solution: Serialize and Deserialize Binary Tree
Solution: Coin Change

Introduction

Given an infinite supply of ‘n’ coin denominations and a total money amount, we are asked to find the total number of distinct ways to make up that amount.

Example:

Denominations: {1,2,3}
Total amount: 5
Output: 5
Explanation: There are five ways to make the change for '5', here are those ways:
  1. {1,1,1,1,1} 
  2. {1,1,1,2} 
  3. {1,2,2}
  4. {1,1,3}
  5. {2,3}

Problem Statement

Given a number array to represent different coin denominations and a total amount 'T', we need to find all the different ways to make a change for 'T' with the given coin denominations. We can assume an infinite supply of coins, therefore, each coin can be chosen multiple times.

Constraints:

  • 1 <= coins.length <= 12
  • 1 <= coins[i] <= 231 - 1
  • 0 <= amount <= 104

This problem follows the "Unbounded Knapsack" pattern.

Basic Solution

A basic brute-force solution could be to try all combinations of the given coins to select the ones that give a total sum of 'T'. This is what our algorithm will look like:

for each coin 'c' create a new set which includes one quantity of coin 'c' if it does not exceed 'T', and recursively call to process all coins create a new set without coin 'c', and recursively call to process the remaining coins return the count of sets who have a sum equal to 'T'

This problem is quite similar to "Count of Subset Sum". The only difference here is that after including the item (i.e., coin), we recursively call to process all the items (including the current coin). In 'Count of Subset Sum', however, we were recursively calling to process only the remaining items.

Code

Here is the code for the brute-force solution:

Java
Saved to Cloud

. . . .

The time complexity of the above algorithm is exponential , where 'C' represents total coin denominations and 'T' is the total amount that we want to make change. The space complexity will be .

Let's try to find a better solution.

Top-down Dynamic Programming with Memoization

We can use memoization to overcome the overlapping sub-problems. We will be using a two-dimensional array to store the results of solved sub-problems. As mentioned above, we need to store results for every coin combination and for every possible sum:

Java
Saved to Cloud

. . . .

Bottom-up Dynamic Programming

We will try to find if we can make all possible sums, with every combination of coins, to populate the array dp[TotalDenominations][Total+1].

So for every possible total ‘t’ (0<= t <= Total) and for every possible coin index (0 <= index < denominations.length), we have two options:

  1. Exclude the coin. Count all the coin combinations without the given coin up to the total 't' => dp[index-1][t]
  2. Include the coin if its value is not more than ‘t’. In this case, we will count all the coin combinations to get the remaining total: dp[index][t-denominations[index]]

Finally, to find the total combinations, we will add both the above two values:

dp[index][t] = dp[index-1][t] + dp[index][t-denominations[index]]

Let’s draw this visually with the following example:

    Denominations: [1, 2, 3]  
    Total: 5 

Let’s start with our base case of zero total:

'0' total can always be found through an empty set
'0' total can always be found through an empty set
total: 1, index:0=> dp[index][total - denominations[index]], since index =0, we didn't consider dp[index-1][total]
total: 1, index:0=> dp[index][total - denominations[index]], since index =0, we didn't consider dp[index-1][total]
total: 2-5, index:0=> dp[index][total - denominations[index]], since index =0, we didn't consider dp[index-1][total]
total: 2-5, index:0=> dp[index][total - denominations[index]], since index =0, we didn't consider dp[index-1][total]
total: 1, index:1=> dp[index-1][total] , we didn't consider dp[index][total - denominations[index]], since 'total < denominations[index]'
total: 1, index:1=> dp[index-1][total] , we didn't consider dp[index][total - denominations[index]], since 'total < denominations[index]'
total: 2, index:1=> dp[index-1][total] + dp[index][total - denominations[index]]
total: 2, index:1=> dp[index-1][total] + dp[index][total - denominations[index]]
total: 3, index:1=> dp[index-1][total] + dp[index][total - denominations[index]]
total: 3, index:1=> dp[index-1][total] + dp[index][total - denominations[index]]
total: 4, index:1=> dp[index-1][total] + dp[index][total - denominations[index]]
total: 4, index:1=> dp[index-1][total] + dp[index][total - denominations[index]]
total: 5, index:1=> dp[index-1][total] + dp[index][total - denominations[index]]
total: 5, index:1=> dp[index-1][total] + dp[index][total - denominations[index]]
total: 5, index:2=> dp[index-1][total] + dp[index][total - denominations[index]]
total: 5, index:2=> dp[index-1][total] + dp[index][total - denominations[index]]

Code

Here is the code for our bottom-up dynamic programming approach:

Java

. . . .

The above solution has time and space complexity of , where 'C' represents total coin denominations and 'T' is the total amount that we want to make change.

Coin Change
Design Add and Search Words Data Structure (medium)
Mark as Completed

On this page

Introduction

Problem Statement

Basic Solution

Code

Top-down Dynamic Programming with Memoization

Bottom-up Dynamic Programming

Code